
$(function(){
	
	
	hideCur();
	var $pg_loader	=	$('#pg_loader');
	var $pg_wrapper	=	$('#pg_wrapper');
	var $bgImage	= 	$('.container_all').children('img:first');
	var $theme = $('.container_all').find('img.pg_bg').length;
	
	
	
	cssprImgs();
	function cssprImgs(){
		//added for css images
		var loadedImgs = $.preloadCssImages({statusTextEl: '#textStatus', statusBarEl: '#status'});
			for(var i = 0; i<loadedImgs.length; i++){
					//$('#loadedImgs').append('<li><img src=\"'+loadedImgs[i].src+'\" />: '+loadedImgs[i].src+'</li>');
					//alert(loadedImgs.length);
				}
				
				setTimeout(function(){
						bgimageAnim();	
						},1000);
		}
	
	function bgimageAnim() {
		$(".pgbackground").animate({opacity:1},1800,'easeOutQuad',null);
		setTimeout(function(){
					themeCheck();
					},600);
		}
	
	
	function themeCheck(){
	if($theme > 0 ){
	
				$('<img>').load(function(){
					//$pg_loader.hide();
					setTimeout(function(){
						themeAvailable();	
						//alert('image check');
						},1000);
					}).attr('src',$bgImage.attr('src'));
					
				} else if($theme == 0) {
						//$pg_loader.hide();
						setTimeout(function(){
						noTheme();	
						//alert('no theme');
						},100);
					}
	
	}
	
	function themeAvailable(){
		//$("body").fadeIn();
		hideCur();
		setTimeout(function(){
				$pg_loader.hide();
					themeBg();
					},1200);
		}
	function noTheme(){
		//$("body").fadeIn();
		hideCur();
		setTimeout(function(){
				$pg_loader.hide();
					showCur();
					SetExternalElementAnim();	
					},300);
		}	

	
	function themeBg(){
		$(".container_all img:first").animate({ width:'980px', height:'890px', left:'0px', right:'0px', top:'0px', opacity:'1.0'},600,'easeOutCubic', null);
		setTimeout(function(){
					showCur();
					SetExternalElementAnim();	
					},600);		
		}
	
	
	function hideCur(){
		$('body').css('cursor','wait');
		
	}
	function showCur(){
		$('body').css('cursor','default');
	setTimeout(function(){ 
			trialSite();
		},4500);	
	}
	
		    //trial close
			function trialSite(){
				$('#trialsite').animate({ opacity:'1.0'},600,'easeOutCubic', null);
				
				}
			var $tclose = $('.tclose');
			$tclose.click(function(){$(this).parent().fadeOut(400);
								return false;
						});		
			//******************//
		
		
function  getMultipleIndex(haystackArr, needle){  

 var indexMatches = new Array();
 var Tmpindex=0;
 var ival=0;

 for(i=0;i<needle.length;i++){
	 
	 
	 if(needle[i]==haystackArr){
		 indexMatches[ival]=i;
		// alert("iii==="+i);
		 ival++;
	 }
	 
 }
 
  return indexMatches;
 }

var  Animk=0;
var OrderID;
var k;
var tmpArray = new Array();


var StartSec=100;
var TmpStartSec=0;


function SetExternalElementAnim(){
	
	
	setInterval(function() {
	
     StartSec = StartSec.toString();
	if(TmpStartSec<10000){
	
	tmpArray=getMultipleIndex(StartSec,homepageelementdurationgapArray);
	//alert("tmpArray.length=" + tmpArray.length);
	if(tmpArray.length>=2){
		for(i=0;i<tmpArray.length;i++){
			SetAnim(tmpArray[i],function(){
				
			});
			isset=true;
		}
	}else{
	
		OrderID = tmpArray[0];
		SetAnim(OrderID,function(){
			
		});
	}
	
	//setTimeout(function(){
		
	//},100);
	StartSec = parseInt( parseInt(StartSec) + 100);
	TmpStartSec = StartSec;
	
	}
}, 100);
	
	
	
	
	/*
	k = Animk+1;
	k = k.toString();
	isset
	
	tmpArray=getMultipleIndex(k,homepageelementdurationgapArray);
	
	if(tmpArray.length>=2){
		//alert(">=2"+tmpArray.length);
		//OrderID = tmpArray[0];
		//homepageelementdurationgapArray[OrderID]=0;
		for(i=0;i<tmpArray.length;i++){
			caa(tmpArray[i]);
			isset=true;
		}
		
		
	}else{
		//alert("=1"+tmpArray.length);
		OrderID = tmpArray[0];
		caa(OrderID);
	}
	*/
	
	/*// OrderID = parseInt($.inArray(k,homepageelementOrderArray));
	AnimateExElement(homepageelementidArray[OrderID],homepageelementAnimArray[OrderID],homepageelementEasingArray[OrderID],homepageelementEasingDurationArray[OrderID],homepageelementdurationgapArray[OrderID],homepageelementAnimDistanceArray[OrderID],function(ExElementID){
		
			
			
			setTimeout(function(){
					
					Animk++;
						if(Animk<totalhomepageExelements){
							SetExternalElementAnim();
						}
			
			
	
		},homepageelementdurationgapArray[OrderID]);
		
	});
	*/
	
	// sleep(1000);
	
	//setTimeout(function(ExElementID){
					
	//	alert("Anim Finished For Element-" + ExElementID);	
	//},500);
	 
	 
 //}

}








function SetAnim(OrderID,callback){
	
	AnimateExElement(homepageelementidArray[OrderID],homepageelementAnimArray[OrderID],homepageelementEasingArray[OrderID],homepageelementEasingDurationArray[OrderID],homepageelementdurationgapArray[OrderID],homepageelementAnimDistanceArray[OrderID],function(ExElementID){
		
			callback();
			
			//setTimeout(function(){
					
				//	Animk++;
						//if(Animk<totalhomepageExelements){
							//SetExternalElementAnim();
						//}
			
			
	
		//},timeduration);
		
	});
}


function DelayAnim(ExElementID){
	
	setTimeout(function(ExElementID){
					
	//	alert("Anim Finished For Element-" + ExElementID);	
	},500);
}


function AnimateExElement(ExElementID,AnimType,EasingType,EasingDuration,durationgap,AnimDistance,callback){
	

		
		/*	setTimeout(function(){
					logoh2();
					},100);
          */


	EasingDuration = parseInt(EasingDuration);
	AnimDistanceMinusVar = "-="+AnimDistance+"px";
	AnimDistancePlusVar = "+="+AnimDistance+"px";
	
	//alert(AnimDistanceMinusVar);
	//alert(AnimDistancePlusVar);
	
	if(AnimType=="LR"){

		$("#"+ExElementID).css({left:AnimDistanceMinusVar}).animate({left:AnimDistancePlusVar,opacity:1},EasingDuration,EasingType,null);
			
		
		
	}else if(AnimType=="RL"){
		
		$("#"+ExElementID).css({left:AnimDistancePlusVar}).animate({left:AnimDistanceMinusVar,opacity:1},EasingDuration,EasingType,null);
		
		/*	setTimeout(function(){
					
			},300);
*/
		
	}else if(AnimType=="TB"){
		
	//	alert("ExElementID="  + ExElementID  +   "   EasingDuration=" + EasingDuration);
		
		$("#"+ExElementID).css({top:AnimDistanceMinusVar}).animate({top:AnimDistancePlusVar,opacity:1},EasingDuration,EasingType,null);
		
		/*	setTimeout(function(){
					
			},300);
*/

	}else if(AnimType=="BT"){
		
		$("#"+ExElementID).css({top:AnimDistancePlusVar}).animate({top:AnimDistanceMinusVar,opacity:1},EasingDuration,EasingType,null);
	
	/*				setTimeout(function(){
					
			},300);
*/

	 }else if(AnimType=="LRTB"){
		
		//$("#"+ExElementID).css({left:'-=300px'}).animate({left:'+=300px',opacity:1},1800,EasingType,null);
		$("#"+ExElementID).css({top:AnimDistanceMinusVar,left:AnimDistanceMinusVar}).animate({top:AnimDistancePlusVar,left:AnimDistancePlusVar,opacity:1},EasingDuration,EasingType,null);
		/*
				setTimeout(function(){
					
			},300);
*/

	}else if(AnimType=="LRBT"){
		
		//$("#"+ExElementID).css({left:'-=300px'}).animate({left:'+=300px',opacity:1},1800,EasingType,null);
		$("#"+ExElementID).css({top:AnimDistancePlusVar,left:AnimDistanceMinusVar}).animate({top:AnimDistanceMinusVar,left:AnimDistancePlusVar,opacity:1},EasingDuration,EasingType,null);
		
		/*			setTimeout(function(){
					
			},300);
*/
		
	}else if(AnimType=="RLTB"){	
	
		//$("#"+ExElementID).css({left:'+=300px'}).animate({left:'-=300px',opacity:1},1800,EasingType,null);
		$("#"+ExElementID).css({top:AnimDistanceMinusVar,left:AnimDistancePlusVar}).animate({top:AnimDistancePlusVar,left:AnimDistanceMinusVar,opacity:1},EasingDuration,EasingType,null);
		
		/*
					setTimeout(function(){
					
			},300);
*/
		
 	}else if(AnimType=="RLBT"){		
	
		//$("#"+ExElementID).css({left:'+=300px'}).animate({left:'-=300px',opacity:1},1800,EasingType,null);
		$("#"+ExElementID).css({top:AnimDistancePlusVar,left:AnimDistancePlusVar}).animate({top:AnimDistanceMinusVar,left:AnimDistanceMinusVar,opacity:1},EasingDuration,EasingType,null);
		/*
			setTimeout(function(){
					
			},300);
*/
		
	}else if(AnimType=="none"){	
	
			//////// Do Nothing
			
			$("#"+ExElementID).css("opacity","0").animate({opacity:1},EasingDuration,EasingType,null);
		
	}
	
	callback(ExElementID);
	
}

	
	
});